package com.lpf.www.service;

import com.lpf.www.common.dto.ResponseMsg;
import com.lpf.www.common.entity.Admin;
import com.lpf.www.common.utils.CommonData;
import com.lpf.www.common.utils.CookieTools;
import com.lpf.www.common.utils.DataHandler;
import com.lpf.www.common.utils.RequestResponseBox;
import com.lpf.www.dao.AdminDao;
import com.lpf.www.utils.CommonMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author lpf
 * @Date 2021/12/30 下午9:16
 * @ProjectName live-streaming-os
 * @Version 1.0
 * @Description:
 */
@Service
public class AdminService {

    private final ResponseMsg responseMsg = new ResponseMsg();
    private final CookieTools cookieTools = new CookieTools();

    private AdminDao adminDao;

    @Autowired
    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    public ResponseMsg adminLogin(Admin admin) {
        //通过用户名来查找用户，然后判断用户输入是否正确
        Admin selectAdminByUsername = adminDao.selectAdminByUsername(admin.getUsername());
        if (selectAdminByUsername != null && admin.getPassword().equals(selectAdminByUsername.getPassword())) {
            responseMsg.setResCode(CommonData.SUCCESS_CODE);
            responseMsg.setResMsg("登录成功！");
            //登录成功后设置该用户的cookie
            CommonMethod.setUserCookie(admin.getUsername(), CommonData.ADMIN_USERNAME, cookieTools);
        } else {
            responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
            responseMsg.setResMsg("登录失败，帐号或用户名错误！");
        }
        return responseMsg;
    }

    /**
     * 当用户登录的时候自动获取帐号和密码
     *
     * @return
     */
    public ResponseMsg getUserInfo() {
        String username = cookieTools.getCookie(CommonData.ADMIN_USERNAME);
        if (username != null && !"".equals(username)) {
            Admin admin = adminDao.selectAdminByUsername(username);
            responseMsg.setResCode(CommonData.SUCCESS_CODE);
            responseMsg.setResMsg("请求成功！");
            responseMsg.setResObj(admin);
        } else {
            responseMsg.setResCode(CommonData.NOT_REMEMBER_ME);
            responseMsg.setResMsg("没有记住密码进行登录");
            responseMsg.setResObj(username);
        }
        return responseMsg;
    }

    /**
     * 管理员注册，将管理员信息插入到数据库中，并且保存到cookie中，当返回登录界面时自动回显
     * @param admin
     * @return
     */
    public ResponseMsg insertAdmin(Admin admin) {
        //将密码进行md5加密
        String password = DigestUtils.md5DigestAsHex(admin.getPassword().getBytes());
        Admin registerAdmin = adminDao.selectAdminByUsername(admin.getUsername());
        if (registerAdmin == null){
            cookieTools.save(CommonData.ADMIN_USERNAME, admin.getUsername(), CommonData.REMEMBER_PASSWORD_TIME);
            admin.setPassword(password);
            int res = adminDao.insertAdmin(admin);
            if (res == 1){
                responseMsg.setResCode(CommonData.SUCCESS_CODE);
                responseMsg.setResMsg("注册成功！");
            }
        } else {
            responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
            responseMsg.setResMsg("该用户名已经被注册，请更换用户名");
        }
        return responseMsg;
    }
}
